var opeType=true;
var bzj = {
    setBtnStatus: function () {
        var row = $('#grid_bzj').datagrid('getSelected');
        if (row && row.status == 0) {   //被选择的应付信用证
            $("#btn_bzj_add").show();
            $("#btn_bzj_mod").show();
            $("#btn_bzj_del").show();
            $("#btn_bzj_qr").show();
            $("#btn_bzj_bzjhz").hide();
        } else if (row && row.status == 1) {   //已确认的保证金
            $("#btn_bzj_add").show();
            $("#btn_bzj_mod").hide();
            $("#btn_bzj_del").hide();
            $("#btn_bzj_qr").hide();
            $("#btn_bzj_bzjhz").show();
        } else {  //未被选择的应付票据
            $("#btn_bzj_add").show();
            $("#btn_bzj_mod").hide();
            $("#btn_bzj_del").hide();
            $("#btn_bzj_qr").hide();
            $("#btn_bzj_bzjhz").hide();
        }
    }
}

$(function () {
    $("#bzj_createDate").datebox();

    $("#bzjgsId").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });

    var sysDate = $("#bzj_sysDate").val();
    //表格初始加载
    $('#grid_bzj').datagrid({
        url: $.webPath() + "/zjgl/bzj/bzjList",
        toolbar: "#toolbar_bzj",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        frozenColumns: [
            [
                {field: 'nbgs_jc', title: '运营商', width: 100, align: 'center'},
                {field: 'bzjlx_z', title: '保证金类型', width: 70, align: 'center'},
                {field: 'cght_htbm', title: '合同编码', width: 150, align: 'center'}
            ]
        ],
        columns: [
            [
                {field: 'kh_jc', title: '客户', width: 150, align: 'center'},
               /* {field: 'ejzh_jc', title: '二级账户', width: 100, align: 'center'},*/
                {field: 'je', title: '保证金(元)', width: 100, align: 'right',
                    formatter: function (value) {
                        if (!value)
                            return "无";
                        else
                            return formatJe2(value, ",");
                    }
                },
                {field: 'ye', title: '保证金余额(元)', width: 100, align: 'right',
                    formatter: function (value) {
                        if (!value)
                            return "无";
                        else
                            return formatJe2(value, ",");
                    }
                },
                {field: 'bzjbl', title: '保证金比例(%)', width: 85, align: 'center'},
                {field: "sk_bm", title: "收款单", width: 100, align: 'right', formatter: function (value, row) {
                    if (value) {
                       // return '<a href="#" id="bzj_bm_sfkbm" class="ye2-linkbutton" style="color:red" sfkBm="' + row["sk_bm"] + '" >' + value + '</a>';
                        return '<a href="#" id="bzj_bm_sfkbm" class="ye2-linkbutton" style="color:red" bzjId="' + row.id + '" >' + value + '</a>';
                    }
                }},
//                {field: "fk_bm", title: "付款单", width: 100, align: 'right', formatter: function (value, row, index) {
//                    if (value) {
//                        return '<a href="#" id="bzj_bm_sfkbm" class="ye2-linkbutton" style="color:red" sfkBm="' + row["fk_bm"] + '" >' + value + '</a>';
//                    }
//                }},
                {field: 'status', title: '状态', width: 70, align: 'right',
                    formatter: function (value) {
                        if (value == 1)
                            return "已锁定";
                        if (value == 0)
                            return "未锁定";
                        if (value == 2)
                            return "已解锁";
                    }
                }
            ]
        ],
        onLoadSuccess: function () {
            bzj.setBtnStatus();
            $(".ye2-linkbutton").linkbutton({plain: true}).unbind("click").bind("click", function () {
                $.window({
                    title: '保证金收款明细',
                    width: 860,
                    height: 480,
                    cache: false,
                    maximizable: true,
                  //  url: $.webPath() + '/zjgl/bzj/bzj_sk_detail?sfkBm=' + $(this).attr("sfkBm"),
                    url: $.webPath() + '/zjgl/bzj/bzj_sk_detail?bzjId=' + $(this).attr("bzjId"),
                    modal: true,
                    buttons: [],
                    onMaximize: function () {
                        $(this).find("#grid_bzj").edatagrid("resize");
                    },
                    onRestore: function () {
                        $(this).find("#grid_bzj").edatagrid("resize");
                    }
                });
            });
        },
        onSelect: function (rowIndex, rowData) {
            bzj.setBtnStatus();
        },
        onCheck: function () {
            bzj.setBtnStatus();
        }
    });

    //**********按钮触发事件**********
    //新增按钮
    $("#btn_bzj_add").click(function () {
        bzj_add('关联收款保证金', 'add_bzj');
    });

    //追加保证金
    $("#btn_bzj_zjbzj").click(function () {
        zj_add('追加收款保证金', 'zj_bzj');
    });

    //查询按钮
    $("#btn_bzj_search").click(function () {
        bzj_search();
    });

    //审核按钮
    $("#btn_bzj_qr").click(function () {
        bzj_confirm();
    });


    //修改保证金
    $("#btn_bzj_mod").click(function () {
        bzj_modefy('修改保证金', 'update_bzj');
    });

    //删除保证金
    $("#btn_bzj_del").click(function () {
        bzj_delete();
    });

    //解锁保证金
    $("#btn_bzj_bzjhz").click(function () {
        bzj_hz();
    });


});
//新增保证金
function bzj_add(title, action) {
    opeType=true;
    var href = $.webPath() + "/zjgl/bzj/bzj_content";
    var actionUrl = $.webPath() + "/zjgl/bzj/add_bzj";
    bzj_showDialog(title, href, actionUrl);
}

//追加保证金
function zj_add(title, action) {
    opeType=false;
    var href = $.webPath() + "/zjgl/bzj/bzj_zj_content";
    var actionUrl = $.webPath() + "/zjgl/bzj/zj_bzj";
    bzj_showDialog(title, href, actionUrl);
}

//修改信用证
function bzj_modefy(title, action) {
    opeType=false;
    var rows = $("#grid_bzj").datagrid("getSelections");
    if (rows.length > 0) {
        if (rows.length > 1) {
            $.messager.alert("提示", "一次只能更新一个记录!", "warning");
            return;
        }
        var href = $.webPath() + "/zjgl/bzj/bzj_content?bzjId=" + rows[0].id;
        var actionUrl = $.webPath() + "/zjgl/bzj/update_bzj";
        bzj_showDialog(title, href, actionUrl);
    } else {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
}

//删除信用证
function bzj_delete() {
    var rows = $("#grid_bzj").datagrid("getSelections");
    if (rows.length > 1) {
        $.messager.alert("提示", "一次只可删除一条数据！", "warning");
        return;
    }
    if (rows) {
        $.messager.confirm('提示', '是否删除该保证金？', function (r) {
            if (r) {
                var index = $('#grid_bzj').datagrid('getRowIndex', rows[0]);
                $.ajax({
                    url: $.webPath() + "/zjgl/bzj/delete_bzj",
                    dataType: 'json',
                    type: 'POST',
                    data: {bzjId: rows[0].id},
                    beforeSend: function () {
                        $("#layout_bzj").mask();
                    },
                    success: function (data) {
                        $("#layout_bzj").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_bzj').datagrid('deleteRow', index);
                            $('#grid_bzj').datagrid('reload');

                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的保证金！", "warning");
    }
}

//解锁保证金
function bzj_hz() {
    var rows = $("#grid_bzj").datagrid("getSelections");
    if (rows.length > 1) {
        $.messager.alert("提示", "一次只能选择一条数据！", "warning");
        return;
    }
    if (rows) {
        $.messager.confirm('提示', '是否解锁保证金?', function (r) {
            if (r) {
                var index = $('#grid_bzj').datagrid('getRowIndex', rows[0]);
                $.ajax({
                    url: $.webPath() + "/zjgl/bzj/hz_bzj",
                    dataType: 'json',
                    type: 'POST',
                    data: {bzjId: rows[0].id},
                    beforeSend: function () {
                        $("#layout_bzj").mask();
                    },
                    success: function (data) {
                        $("#layout_bzj").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_bzj').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要划转的保证金！", "warning");
    }
}


//弹出新增或修改的窗口
function bzj_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 400,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_bzj').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_bzj').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//确认保证金
function bzj_confirm() {
    var row = $('#grid_bzj').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否确认该保证金？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/bzj/qr_bzj",
                    dataType: 'json',
                    type: 'POST',
                    data: {bzjId: row.id},
                    beforeSend: function () {
                        $("#layout_bzj").mask();
                    },
                    success: function (data) {
                        $("#layout_bzj").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_bzj').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要确认的保证金！", "warning");
    }
}


//查询保证金
function bzj_search() {
    var gsMc = $("#bzjgsId").combotree('getText') == null ? null : $("#bzjgsId").combotree('getText');
    var gsId = $("#bzjgsId").combotree('getValue') == null ? null : $("#bzjgsId").combotree('getValue');
    var hth = $("#bzj_hth").val();
    var khMc = $("#bzj_kh").val();
    var createDate = $("#bzj_createDate").datebox("getValue");
    $('#grid_bzj').datagrid('load',
        {
            gsId: gsId,
            gsMc: gsMc,
            hth: hth,
            khMc: khMc,
            createDate: createDate
        }
    );
}
